package com.whmskj.xjlhsc.schedul.async.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whmskj.xjlhbc.core.Page;
import com.whmskj.xjlhsc.schedul.async.mapper.YiqianjiReportMapper;
import com.whmskj.xjlhsc.schedul.async.po.YiqianjiReport;
import com.whmskj.xjlhsc.schedul.async.service.YiqianjiReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author lk
 * @since 2025-07-14 09:54:47
 */
@Service
@Slf4j
public class YiqianjiReportServiceImpl extends ServiceImpl<YiqianjiReportMapper, YiqianjiReport> implements YiqianjiReportService {

    @Autowired
    private YiqianjiReportMapper yiqianjiReportMapper;

    @Override
    public List<YiqianjiReport> findPage(YiqianjiReport params) {
        Page.startPage();
        LambdaQueryWrapper<YiqianjiReport> query = Wrappers.lambdaQuery(YiqianjiReport.class);
        List<String> dateRange = params.getDateRange();
        if(!dateRange.isEmpty()) {
            query.between(YiqianjiReport::getDateYmd, dateRange.get(0), dateRange.get(1));
        }
        query.eq(StringUtils.hasText(params.getDateYmd()), YiqianjiReport::getDateYmd, params.getDateYmd());
        query.eq(Objects.nonNull(params.getDeviceId()), YiqianjiReport::getDeviceId, params.getDeviceId());
        query.orderByDesc(YiqianjiReport::getDateYmd).orderByAsc(YiqianjiReport::getDeviceName);
        return list(query);
    }

    @Override
    public List<YiqianjiReport> findList(YiqianjiReport params){
        LambdaQueryWrapper<YiqianjiReport> query = Wrappers.lambdaQuery(YiqianjiReport.class);
        List<String> dateRange = params.getDateRange();
        if(!dateRange.isEmpty()) {
            query.between(YiqianjiReport::getDateYmd, dateRange.get(0), dateRange.get(1));
        }
        query.eq(StringUtils.hasText(params.getDateYmd()), YiqianjiReport::getDateYmd, params.getDateYmd());
        query.eq(Objects.nonNull(params.getDeviceId()), YiqianjiReport::getDeviceId, params.getDeviceId());
        return list(query);
    }

    @Override
    public YiqianjiReport findById(Long id) {
        return yiqianjiReportMapper.selectById(id);
    }

    @Override
    public boolean insert(YiqianjiReport yiqianjiReport) {
        return save(yiqianjiReport);
    }

    @Override
    public boolean update(YiqianjiReport yiqianjiReport) {
        return updateById(yiqianjiReport);
    }

    @Override
    public int delete(Long id) {
        return yiqianjiReportMapper.deleteById(id);
    }


    @Override
    public YiqianjiReport rpQuery(LocalDateTime start,LocalDateTime end, String tb) {
        return yiqianjiReportMapper.rpQuery(start,end,tb);
    }
}
